package com.zqwzd.action.json;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.zqwzd.entity.User;
import com.zqwzd.service.IUserService;
import com.zqwzd.util.DataExchangeModel;

public class UserJsonAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private IUserService userService;

	private User user = new User();
	

	// 查询单个示例
	public String getUserJson() {
		JSONArray jsonArray	= null;
		DataExchangeModel dataExchangeModel = new DataExchangeModel();
		try {
			User loginUser = userService.login(user);
			dataExchangeModel.setResponseState(0);
			dataExchangeModel.setResponseData(loginUser);
			HttpServletResponse	res	=	ServletActionContext.getResponse();
			res.setHeader("Access-Control-Allow-Origin", "*");
			res.setCharacterEncoding("utf-8");
			res.setContentType("text/json");
			jsonArray	=	JSONArray.fromObject(dataExchangeModel);
			res.getWriter().write(jsonArray.toString());
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	}


	// 查询全部
	public String search() {
		JSONArray jsonArray	= null;
		DataExchangeModel dataExchangeModel = new DataExchangeModel();
		try {
			List<User> users = userService.getAllUser();
			
			dataExchangeModel.setResponseState(0);
			dataExchangeModel.setResponseData(users);
			HttpServletResponse	res	=	ServletActionContext.getResponse();
			res.setCharacterEncoding("utf-8");
			res.setContentType("text/json");
			jsonArray	=	JSONArray.fromObject(dataExchangeModel);
			res.getWriter().write(jsonArray.toString());
			
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return null;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
